from socket import socket, AF_INET, SOCK_STREAM

# AF_INET AF_INET(TCP/IP-IPv4)
# 流式套接字（SOCK_STREAM)


# 1创建socket对象
server_socket = socket(AF_INET, SOCK_STREAM)
# 2进行绑定ip和端口
ip = 'localhost'
port = 8887
server_socket.bind((ip, port))
# 3进行监听
server_socket.listen(5)
print("服务端启动")
# 4接受连接,阻塞等待
client_socket, client_address = server_socket.accept()
print("client_socket", client_socket)
print("client_address", client_address)
# 5接收的数据
rev_data = client_socket.recv(1024)
print("接受的数据", rev_data.decode())
# 发送数据给客户端
client_socket.send("服务端给你的消息，欢迎，新朋友".encode())

# 6 服务端套接字进行关闭
server_socket.close()
